package com.sanjiahantang.ythshopping.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sanjiahantang.shopping.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.shopping.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.adapter.home.MyGridView;
import com.sanjiahantang.ythshopping.model.AreaBean;
import com.sanjiahantang.ythshopping.utils.Utils;
import com.sanjiahantang.ythshopping.views.SelectAreaView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zy on 2017/9/18.
 */

public class NewAreaListAdapter extends BaseAdapter {

    private List<AreaBean.City> mData,hotCity;
    private LayoutInflater inflater;
    private HashMap<String, Integer> letterIndexes;
    private String[] sections;
    private static final int VIEW_TYPE_COUNT = 4;
    private Context mContext;
    private OnCityClickListener onCityClickListener;
    private String locatedCity;
    private String locatedProvince;
    private int locateState;
    public NewAreaListAdapter(Context mContext, List<AreaBean.City> data, List<AreaBean.City> hotCity){
        if (data == null){
            data = new ArrayList<>();
        }
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        this.mData = data;
        this.hotCity = hotCity;

        //排序
        Collections.sort(mData, new AreaBean.CompareSort());
        mData.add(0,new AreaBean.City("GPS定位","0"));
        mData.add(1,new AreaBean.City("历史访问城市","1"));
        mData.add(2,new AreaBean.City("国内热门城市","2"));

        int size = mData.size();
        letterIndexes = new HashMap<>();
        sections = new String[size];
        for (int index = 0; index < size; index++){
            //当前城市拼音首字母
            String currentLetter = Utils.getFirstLetter(mData.get(index).getPinyin());
            //上个首字母，如果不存在设为""
            String previousLetter = index >= 1 ? Utils.getFirstLetter(mData.get(index - 1).getPinyin()) : "";
            if (!TextUtils.equals(currentLetter, previousLetter)){
                letterIndexes.put(currentLetter, index);
                sections[index] = currentLetter;
            }
        }
    }
    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData == null ? null : mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CityViewHolder holder;
        int viewType = getItemViewType(position);
        switch (viewType){
            case 0:     //定位
                convertView = inflater.inflate(R.layout.item_area_locate, parent, false);
                TextView gps = (TextView) convertView.findViewById(R.id.txtGPS);
                TextView state = (TextView) convertView.findViewById(R.id.txtGPSCity);
                switch (locateState){
                    case SelectAreaView.LOCATING:
                        state.setText(mContext.getString(R.string.cp_locating));
                        break;
                    case SelectAreaView.FAILED:
                        state.setText(R.string.cp_located_failed);
                        break;
                    case SelectAreaView.SUCCESS:
                        state.setText(locatedCity);
                        break;
                }
                gps.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (locateState == SelectAreaView.FAILED){
                            //重新定位
                            if (onCityClickListener != null){
                                onCityClickListener.onLocateClick();
                            }
                        }else if (locateState == SelectAreaView.SUCCESS){
                            //返回定位城市
                            if (onCityClickListener != null){
                                AreaBean.City cityLocate = new AreaBean.City();
                                cityLocate.setRegion_name(locatedCity);
                                cityLocate.setParent_name(locatedProvince);
                                onCityClickListener.onCityClick(cityLocate);
                            }
                        }
                    }
                });
                break;
//                ViewGroup container = (ViewGroup) convertView.findViewById(R.id.layout_locate);
//                TextView state = (TextView) convertView.findViewById(R.id.tv_located_city);
//                switch (locateState){
//                    case SelectAreaView.LOCATING:
//                        state.setText(mContext.getString(R.string.cp_locating));
//                        break;
//                    case SelectAreaView.FAILED:
//                        state.setText(R.string.cp_located_failed);
//                        break;
//                    case SelectAreaView.SUCCESS:
//                        state.setText(locatedCity);
//                        break;
//                }
//                container.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        if (locateState == SelectAreaView.FAILED){
//                            //重新定位
//                            if (onCityClickListener != null){
//                                onCityClickListener.onLocateClick();
//                            }
//                        }else if (locateState == SelectAreaView.SUCCESS){
//                            //返回定位城市
//                            if (onCityClickListener != null){
//                                onCityClickListener.onCityClick(locatedCity);
//                            }
//                        }
//                    }
//                });
            case 1:     //历史
                convertView = inflater.inflate(R.layout.item_area_history, parent, false);

                MyGridView historyGrid = (MyGridView) convertView.findViewById(R.id.gvHistoryList);
                List<AreaBean.City> historyCity = AreaBean.getHistory(mContext);
                historyGrid.setAdapter(new QuickAdapter<AreaBean.City>(mContext,R.layout.item_area_hot_city,historyCity) {
                    @Override
                    protected void convert(BaseAdapterHelper helper, final AreaBean.City item) {
                        helper.setText(R.id.tv_hot_city_name,item.getRegion_name());
                        helper.setOnClickListener(R.id.tv_hot_city_name, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (onCityClickListener != null){
                                    onCityClickListener.onCityClick(item);
                                }
                            }
                        });
                    }
                });
                break;
            case 2:
                convertView = inflater.inflate(R.layout.item_area_hot, parent, false);
                MyGridView hotGrid = (MyGridView) convertView.findViewById(R.id.gvHotList);
                hotGrid.setAdapter(new QuickAdapter<AreaBean.City>(mContext,R.layout.item_area_hot_city,hotCity) {
                    @Override
                    protected void convert(BaseAdapterHelper helper, final AreaBean.City item) {
                        helper.setText(R.id.tv_hot_city_name,item.getRegion_name());
                        helper.setOnClickListener(R.id.tv_hot_city_name, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (onCityClickListener != null){
                                    onCityClickListener.onCityClick(item);
                                }
                            }
                        });

                    }
                });
                break;
            case 3:     //所有
                if (convertView == null){
                    convertView = inflater.inflate(R.layout.item_area_city, parent, false);
                    holder = new CityViewHolder();
                    holder.letter = (TextView) convertView.findViewById(R.id.tv_item_city_listview_letter);
                    holder.name = (TextView) convertView.findViewById(R.id.tv_item_city_listview_name);
                    convertView.setTag(holder);
                }else{
                    holder = (CityViewHolder) convertView.getTag();
                }
                if (position >= 1){
                    final AreaBean.City city = mData.get(position);
                    holder.name.setText(city.getRegion_name());
                    String currentLetter = Utils.getFirstLetter(mData.get(position).getPinyin());
                    String previousLetter = position >= 1 ? Utils.getFirstLetter(mData.get(position - 1).getPinyin()) : "";
                    if (!TextUtils.equals(currentLetter, previousLetter)){
                        holder.letter.setVisibility(View.VISIBLE);
                        holder.letter.setText(currentLetter);
                    }else{
                        holder.letter.setVisibility(View.GONE);
                    }
                    holder.name.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (onCityClickListener != null){
                                onCityClickListener.onCityClick(city);
                            }
                        }
                    });
                }
                break;
        }
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        return position < VIEW_TYPE_COUNT - 1 ? position : VIEW_TYPE_COUNT - 1;
    }


    public static class CityViewHolder{
        TextView letter;
        TextView name;
    }

    public void setOnCityClickListener(OnCityClickListener listener){
        this.onCityClickListener = listener;
    }

    public interface OnCityClickListener{
        void onCityClick(AreaBean.City city);
        void onLocateClick();
    }

    /**
     * 获取字母索引的位置
     * @param letter
     * @return
     */
    public int getLetterPosition(String letter){
        Integer integer = letterIndexes.get(letter);
        return integer == null ? -1 : integer;
    }

    /**
     * 更新定位状态
     * @param state
     */
    public void updateLocateState(int state, String city,String province){
        this.locateState = state;
        this.locatedCity = city;
        this.locatedProvince = province;
        notifyDataSetChanged();
    }
}
